package com.tian.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * {@code @description:} TODO
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-06-11 16:27
 * {@code @version:} 1.0
 */
public class Demo {

    private void test() {
        synchronized (Demo.class) {
            System.out.println("test");
        }
    }

    private static synchronized void test1() {
        System.out.println("test");
    }

    private void test2() {
        synchronized (this) {
            System.out.println("test");
        }
    }
    // 根据Example 获取Class =》Example.class
    public Class<Demo> getDemoClass(){
        Class<Demo> clazz = Demo.class;
        return clazz;
    }
    public Demo getDemoInstance ()throws Exception{
        Class class1 = Class.forName("reflection.Student");
        Demo demo = (Demo)class1.newInstance();

        Field setUserName = class1.getDeclaredField("setUserName");
        setUserName.setAccessible(true);
        setUserName.set(demo,"佛系老田");

        Method setUserName1 = class1.getDeclaredMethod("setUserName");
        setUserName1.setAccessible(true);
        setUserName1.invoke(demo,"佛系老田");

        return demo;
    }
}
